package com.qq.BFMRSE.index;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import com.qq.BFMRSE.entity.encryptedIndex;
import com.qq.BFMRSE.entity.wordCountInfo;
import com.qq.BFMRSE.extractWord.SelectWord;
import com.qq.BFMRSE.serialize.objectSerializeUtil;
import com.qq.BFMRSE.util.BFConstants;
import com.qq.BFMRSE.util.UserParamManager;

import Jama.Matrix;
public class indexEncrypted implements Serializable{
	private static final long serialVersionUID = 9L;
	private  int []S;
	private  Matrix M1;
	private  Matrix M2;//秘钥三元组用于加密索引向量
    public int[] getS() {
		return S;
	}
	public void setS(int[] s) {
		S = s;
	}
	public Matrix getM1() {
		return M1;
	}
	public void setM1(Matrix m1) {
		M1 = m1;
	}
	public Matrix getM2() {
		return M2;
	}
	public void setM2(Matrix m2) {
		M2 = m2;
	}
    public indexEncrypted()
    {
    	System.out.println("test indexEncrypted() start");
    	//int n=indexHelper.wordList.size()+BFConstants.DUMMUYKWYWORDCOUNT;//100个空位置中前2v个作为dummuy keyword,后80作为用于更新时使用
    	int n=UserParamManager.getVectorLength();
    	SelectWord selectWord=null;
		try {
			selectWord = (SelectWord) objectSerializeUtil.deserialize(BFConstants.WORDINFOFILE+File.separator+BFConstants.INITKEYVALUE);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(selectWord!=null)
		{
		 	n=selectWord.getN();
		}
    	System.out.println("M1.x="+n);
    	S=indexBuildTool.getRandomArray(n);
		M1=Matrix.random(n, n);
		M2=Matrix.random(n, n);
    }
    /**通过分块对角阵对原来秘钥进行扩展
     * 
     * @param extSize
     */
    public void genExtSK(int extSize){
    	int n=UserParamManager.getVectorLength();
    	Matrix M12=Matrix.random(extSize, extSize);
    	Matrix M22=Matrix.random(extSize, extSize);
    	Matrix M11=new Matrix(n, n);
    	Matrix M21=new Matrix(n, n);//长度为n*n的零向量
    	int orN=M1.getColumnDimension();
    	int i=0;
    	int j=0;
    	for(i=0;i<orN;i++)
    		for(j=0;j<orN;j++){
    			M11.set(i, j, M1.get(i, j));
    			M21.set(i, j, M2.get(i, j));
    		}
    	for(i=orN;i<n;i++)
    		for(j=orN;j<n;j++){
    			M11.set(i, j, M12.get(i, j));
    			M21.set(i, j, M22.get(i, j));
    		}
    	M1=M11;
    	M2=M21;
    }
	/**
	 * 对明文索引plainIndex按照KNN进行加密
	 * @param plainIndex
	 * @return
	 */
	public  encryptedIndex IndexEncrypted(double[] plainIndex)
	{
		encryptedIndex resultEncryptedIndex=new encryptedIndex();
		double []tempMD1=new double[plainIndex.length];
		double []tempMD2=new double[plainIndex.length];
		for(int i=0;i<S.length;i++)
		{
			//如果S[i]==0,那么tempMD1[i]=tempMD2[i]=plainIndex[i]
			if(S[i]==0)
			{
				tempMD1[i]=tempMD2[i]=plainIndex[i];
			}
			//如果S[i]==1,那么tempMD1[i]+tempMD2[i]=plainIndex[i]
			else {
				double []sp=indexBuildTool.splitDouble(plainIndex[i]);
				tempMD1[i]=sp[0];
				tempMD2[i]=sp[1];
			}
		}
		System.out.println("test Matrix.length="+M1.getColumnDimension()*M1.getRowDimension());
		System.out.println("tempMD1.size()="+tempMD1.length);
		Matrix encryMatrix1=M1.transpose().times(new Matrix(tempMD1,1).transpose());
		Matrix encryMatrix2=M2.transpose().times(new Matrix(tempMD2,1).transpose());
		tempMD1=indexBuildTool.getArrayFromMatrix(encryMatrix1);
		tempMD2=indexBuildTool.getArrayFromMatrix(encryMatrix2);
		resultEncryptedIndex.setMD1(tempMD1);
		resultEncryptedIndex.setMD2(tempMD2);
		return resultEncryptedIndex;
	}
}